package com.algrithom.pointer;

import java.util.Arrays;

/**
 * 283. 移动零
 * 
 * @author think
 */
public class Solution2 {
    
    public static void main(String[] args){
        int[] nums = new int[]{0,1,0,3,12};
        //        int[] nums = new int[]{0,0};
        Solution2 solution2 = new Solution2();
        solution2.moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }
    
    public void moveZeroes(int[] nums){
        int len = nums.length;
        int right = len - 1;
        while (right > 0) {
            while (nums[right] != 0 && right > 0) {
                right--;
            }
            int index = right;
            while (index + 1 < len) {
                nums[index] = nums[index] ^ nums[index + 1];
                nums[index + 1] = nums[index] ^ nums[index + 1];
                nums[index] = nums[index] ^ nums[index + 1];
                index++;
            }
            right--;
        }
    }
    
    public void moveZeroes2(int[] nums){
        int len = nums.length;
        for (int i = 1; i <= len; i++) {
            for (int j = 0; j < len - 1; j++) {
                if (nums[j] == 0) {
                    nums[j] = nums[j] ^ nums[j + 1];
                    nums[j + 1] = nums[j] ^ nums[j + 1];
                    nums[j] = nums[j] ^ nums[j + 1];
                }
            }
        }
    }
}
